# 课堂练习：
# 添加话术——》修改话术——》删除你刚新增的话术
# 9人未读
# t_url="http://60.205.190.66:8899"
# import requests
# import random
# #登陆
# tt=requests.post(url=t_url+"/adminapi/login",json={"account":"admin","pwd":"920509","imgcode":"5"})
# token=tt.json()['data']['token']
# # print(token)
# #添加话丨
# num = random.randint(1,1000)
# ko=requests.post(url=t_url+"/adminapi/app/wechat/speechcraft",json={"cate_id":0,"title":"秒速五厘米" + str(num),"message":f"“樱花落下的速度，是每秒五厘米。那我用什么样的速度才能与你相遇？{num}”","sort":0},headers={"Authori-zation":"Bearer "+str(token)})
# msg=ko.json()['msg']
# print(str(num))
# # print(ko.text)
# assert msg=="创建话术成功"
# # 获取id
# ko5 = requests.get(url= t_url + "/adminapi/app/wechat/speechcraft?page=1&limit=1000&cate_id=",headers={"Authori-zation":"Bearer "+str(token)})
# # print(ko5.text)
# # id = ko5.json()['data']['list'][0]['id']
# id_list=[]
# for i in ko5.json()['data']['list']:
#     if i['title'] == '秒速五厘米' + str(num):
#         id_list.append(i['id'])
#
# # 修改花束
# ko1=requests.put(url=t_url+f"/adminapi/app/wechat/speechcraft/" + str(id_list[0]),json={"cate_id":0,"title":f"知名语录{num}","message":f"“故友的灵魂，可以交给我吗？{num}？”","sort":0},headers={"Authori-zation":"Bearer "+str(token)})
# assert ko1.json()['msg']=="修改成功"
# print(ko1.status_code)
# print(ko1.text)
# # for i in ko1.json()['data']['list']
# # 删除花束
# ko7=requests.delete(url=t_url+f"/adminapi/app/wechat/speechcraft/" + str(id_list[0]),headers={"Authori-zation":"Bearer "+str(token)})
# assert ko7.json()['msg']=="删除成功"
# print(ko7.status_code)
# print(ko7.text)


# 作业：
# 1, 操作
# 业务1：登录->用户分组—>添加分组->删除分组

# 业务3：登录->商品->商品分类->添加分类->删除分类
# 2、使用pytest框架做，要有setup和teardown

import pytest
import requests
from faker import Faker

class Test_qa():
    t_url = "http://60.205.190.66:8899"
    faker = Faker(locale='zh_cn')
    name = faker.name()
    id_list = []

    @pytest.fixture(scope="function", autouse=True)  # todo: autouse：打开自动开关，可以让所有的用例找到此方法并执行
    def login(self):
        res = requests.post(self.t_url + "/adminapi/login", json={"account": "admin", "pwd": "920509", "imgcode": "x"})
        self.token = res.json()['data']['token']
        yield
        requests.get(self.t_url + '/adminapi/setting/admin/logout')

    def test_a(self):

        res1 = requests.post(url=self.t_url + "/adminapi/user/user_group/save.html",
                             json={"group_name": 'yys'+str(self.name)},
                             headers={"Authori-Zation": 'Bearer ' + str(self.token)})
        print(res1.json())

    def test_b(self):
        res2 = requests.get(self.t_url + "/adminapi/user/user_group/list?page=1&limit=100",
                            headers={"Authori-Zation": 'Bearer ' + str(self.token)})
        for i in res2.json()['data']['list']:
            if i["group_name"] == 'yys'+str(self.name):
                self.id_list.append(i['id'])
        print(self.id_list[0])
    def test_c(self):
        res3 = requests.delete(url=self.t_url + "/adminapi/user/user_group/del/" + str(self.id_list[0]),
                             headers={"Authori-Zation": 'Bearer ' + str(self.token)})
        print(res3.json())


if __name__ == '__main__':
    pytest.main()

# 业务2：登录->客服->客服话术->添加话术->删除话术
import pytest
import requests
import random


class TestSpeechcraft():
    t_url = "http://60.205.190.66:8899"
    num = random.randint(1, 1000)
    id_list = []
    @pytest.fixture(scope="function", autouse=True)
    def login(self):
        resw = requests.post(self.t_url + "/adminapi/login", json={"account": "admin", "pwd": "920509", "imgcode": "x"})
        self.token = resw.json()['data']['token']
        yield
        requests.get(self.t_url + '/adminapi/setting/admin/logout')

    def test_a(self):
        res1 = requests.post(self.t_url + "/adminapi/app/wechat/speechcraft",
                             json={"cate_id": 0, "title": "秒速五厘米",
                                   "message": '樱花落下的速度，是每秒五厘米。那我用什么样的速度才能与你相遇' + str(self.num), "sort": 0},
                             headers={"Authori-Zation": 'Bearer ' + str(self.token)})
        print(res1.json())

    def test_b(self):
        res2 = requests.get(self.t_url + "/adminapi/app/wechat/speechcraft?page=1&limit=100&cate_id=",
                            headers={"Authori-Zation": 'Bearer ' + str(self.token)})
        # print(res2.json()['data']['list'])
        for i in res2.json()['data']['list']:
            if i["message"] == '樱花落下的速度，是每秒五厘米。那我用什么样的速度才能与你相遇' + str(self.num):
                self.id_list.append(i['id'])

    def test_c(self):
        res3 = requests.delete(self.t_url + "/adminapi/app/wechat/speechcraft/" + str(self.id_list[0]),
                               headers={"Authori-Zation": 'Bearer ' + str(self.token)})
        print(res3.json())

if __name__ == '__main__':
    pytest.main()
